package com.shqcjd.siemensglass;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.google.gson.Gson;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.shqcjd.siemensglass.utils.PrefUtils;
import com.shqcjd.siemensglass.utils.UIUtils;
import com.shqcjd.siemensglass.widget.whiteboard.DeveloperInfo;

import org.ar.arboard.ARBoardEngine;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ceyes.services.voice.GlassVoiceService;
import io.agora.AgoraAPIOnlySignal;
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;

/**
 * Created by sy on 2018/11/22.
 */

public class MyApp extends MultiDexApplication {
    private GlassVoiceService mVoiceService = GlassVoiceService.getInstance();
    public static Context applicationContext;
    private AgoraAPIOnlySignal m_agoraAPI;
    private RtcEngine mRtcEngine;
    private static MyApp mInstance;
    private static Context mContext;
    private String appID;

    public static MyApp the() {
        return mInstance;
    }

    public MyApp() {
        mInstance = this;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        MultiDex.install(this);
        super.onCreate();
        mContext = this.getApplicationContext();
        //日志
        Logger.addLogAdapter(new AndroidLogAdapter());
        applicationContext = this.getApplicationContext();

        setupAgoraEngine();

        //any电子白板
        //配置开发者信息
        ARBoardEngine.Inst().initEngine( DeveloperInfo.APPID, DeveloperInfo.APPTOKEN);
        ARBoardEngine.Inst().setDebugLog(true);


        // define your own keywords
//        ArrayList<String> keywords = new ArrayList<String>();
//        keywords.add("玩游戏");
//        keywords.add("发微信");
        Map<String, Integer> map = new HashMap<>();
//        map.put("语音识别", 20);
//        map.put("打开", 20);

//        mVoiceService.startService(this, keywords);
//        Map<语音词,置信度（默认20）>
        mVoiceService.startService(map, this);
    }

    public static Context getmContext() {
        return mContext;
    }



    public RtcEngine getmRtcEngine() {
        return mRtcEngine;
    }

    public AgoraAPIOnlySignal getmAgoraAPI() {
        return m_agoraAPI;
    }


    private OnAgoraEngineInterface onAgoraEngineInterface;

    //声网
    private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
        @Override
        public void onFirstRemoteVideoDecoded(final int uid, int width, int height, int elapsed) {
            if (onAgoraEngineInterface != null) {
                onAgoraEngineInterface.onFirstRemoteVideoDecoded(uid, width, height, elapsed);
            }
        }

        @Override
        public void onUserJoined(int uid, int elapsed) {
            if (onAgoraEngineInterface != null) {
                onAgoraEngineInterface.onUserJoined(uid, elapsed);
            }
        }

        @Override
        public void onUserOffline(int uid, int reason) {
            Logger.i("onUserOffline uid: " + uid + " reason:" + reason);
            if (onAgoraEngineInterface != null) {
                onAgoraEngineInterface.onUserOffline(uid, reason);
            }
        }

        @Override
        public void onUserMuteVideo(final int uid, final boolean muted) {
            if (onAgoraEngineInterface != null) {
                onAgoraEngineInterface.onUserMuteVideo(uid, muted);
            }
        }

        @Override
        public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
            super.onJoinChannelSuccess(channel, uid, elapsed);
            Logger.i( "onJoinChannelSuccess channel:" + channel + " uid:" + uid);
            if (onAgoraEngineInterface != null) {
                onAgoraEngineInterface.onJoinChannelSuccess(channel, uid, elapsed);
            }
        }

        @Override
        public void onNetworkQuality(int uid, int txQuality, int rxQuality) {
            super.onNetworkQuality(uid, txQuality, rxQuality);
            if (onAgoraEngineInterface != null) {
                onAgoraEngineInterface.onNetworkQuality(uid, txQuality, rxQuality);
            }
        }
    };
    private void setupAgoraEngine() {
        //多声网id模式
//        String appidListJson = PrefUtils.getString(this, PrefUtils.APPID_LIST, null);
//        if (appidListJson != null) {
//            List<String> list = new Gson().fromJson(appidListJson, List.class);
//            String agoraAppId = UIUtils.getAgoraAppId(list);
//            if (agoraAppId != null) {
//                appID = agoraAppId;
//            } else {
////            appID = getString(R.string.agora_app_id);
//                List<String> idList = Arrays.asList(getResources().getStringArray(R.array.agora_appid_list));
//                appID = UIUtils.getAgoraAppId(idList);
//            }
//        } else {
//            List<String> idList = Arrays.asList(getResources().getStringArray(R.array.agora_appid_list));
//            appID = UIUtils.getAgoraAppId(idList);
//        }

        //单id
        appID = getString(R.string.agora_app_id);

        PrefUtils.setString(this, PrefUtils.AGORA_APPID, appID);
        Logger.i("声网appid:"+appID);

        try {
            m_agoraAPI = AgoraAPIOnlySignal.getInstance(this, appID);
            mRtcEngine = RtcEngine.create(getBaseContext(), appID, mRtcEventHandler);
        } catch (Exception e) {
            throw new RuntimeException("NEED TO check rtc sdk init fatal error\n" + Log.getStackTraceString(e));
        }
    }

    public void setOnAgoraEngineInterface(OnAgoraEngineInterface onAgoraEngineInterface) {
        this.onAgoraEngineInterface = onAgoraEngineInterface;
    }


    public interface OnAgoraEngineInterface {
        void onFirstRemoteVideoDecoded(final int uid, int width, int height, int elapsed);

        void onUserJoined(int uid, int elapsed);

        void onUserOffline(int uid, int reason);

        void onUserMuteVideo(final int uid, final boolean muted);

        void onJoinChannelSuccess(String channel, int uid, int elapsed);

        void onNetworkQuality(int uid, int txQuality, int rxQuality);
    }
}